package com.eshanren.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

/**
 * http工具类
 *
 * @author WWF
 */
public class HttpUtil {

    /**
     * 用户代理*
     */
    public static final String USER_AGENT = "User-Agent";
    /**
     * 未知
     */
    public static final String UN_KNOWN = "unknown";

    /**
     * 获取ip地址
     *
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        if (request == null) {
            return "";
        }

        String ip = request.getHeader("X-Forwarded-For");
        if (StringUtil.isNotEmpty(ip)) {
            String[] ips = ip.split(",");
            for (int i = 0; i < ips.length; i++) {
                String realIp = ips[i];
                if (StringUtil.isNotEmpty(realIp) && !"unknown".equalsIgnoreCase(realIp.trim())) {
                    ip = realIp;
                    break;
                }
            }
        }
        if (ip == null || ip.length() == 0 || UN_KNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UN_KNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UN_KNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || UN_KNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || UN_KNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }


    /**
     * 判断是否是POST
     *
     * @param request
     * @return
     */
    public static boolean isPost(HttpServletRequest request) {
        return "POST".equals(request.getMethod().toUpperCase(Locale.CHINA));
    }

    /**
     * http 默认端口
     */
    public static final int HTTP_PORT = 80;
    /**
     * https 默认端口
     */
    public static final int HTTPS_PORT = 443;

    /**
     * 获得项目的绝对地址，不以'/'结尾
     */
    public static String getBasePath(HttpServletRequest request, boolean isHttps) {
        String path = request.getContextPath();
        int serverPost = request.getServerPort();
        String basePath;
        if (isHttps) {
            basePath = "https://";
        } else {
            basePath = "http://";
        }
        if (serverPost == HTTP_PORT || serverPost == HTTPS_PORT) {
            basePath += request.getServerName() + path;
        } else {
            basePath += request.getServerName() + ":"
                    + request.getServerPort() + path;
        }
        return basePath;
    }

    /**
     * Get cookie object by cookie name.
     */
    public static Cookie getCookieObject(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    return cookie;
                }
            }
        }
        return null;
    }


    /**
     * 判断用户的浏览器是不是ie浏览器
     *
     * @param request
     * @return
     */
    public static boolean isIE(HttpServletRequest request) {
        String userAgrent = request.getHeader(USER_AGENT).toUpperCase();
        return userAgrent.contains("MSIE");
    }


    /**
     * 判断请求是否是ajax
     *
     * @param request
     * @return
     */
    public static boolean isAjax(HttpServletRequest request) {
        String requestType = request.getHeader("X-Requested-With");
        return StringUtil.isNotEmpty(requestType);
    }

    /**
     * http 前缀
     */
    public static final String HTTP_PREFIX = "http://";
    /**
     * https 前缀
     */
    public static final String HTTPS_PREFIX = "https://";

    /**
     * URL检查
     *
     * @param pInput 要检查的字符串
     * @return boolean   返回检查结果
     */
    public static boolean isUrl(String pInput) {
        if (pInput == null) {
            return false;
        }
        if (pInput.startsWith(HTTP_PREFIX) || pInput.startsWith(HTTPS_PREFIX)) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        String url = "http://mp.weixin.qq.com";
        System.out.println(isUrl(url));

    }

}
